package edu.uwosh.cs342.placestobe;


import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class NewUser extends Activity{
	String newUser, newPass, adminPass;
	EditText etNewUser, etNewPass, etAdminPass;
	Button saveNewUserButton;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		setContentView(R.layout.new_user);
		etNewUser = (EditText) findViewById(R.id.userNameEditText);
		etNewPass = (EditText) findViewById(R.id.passwordEditText);
		
		saveNewUserButton = (Button) findViewById(R.id.save_user);
		saveNewUserButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v){
				addUser();
			}
		});
	}
	
	public void addUser(){
		newUser = etNewUser.getText().toString();
		newPass = etNewPass.getText().toString();
			if (newUser.length() > 0 && newPass.length() > 0){
				new AsyncAddUser().execute((Object[]) null);
			}else{
				Toast.makeText(getApplicationContext(), "Fill all fields", Toast.LENGTH_SHORT).show();
			}
		
	}
	

	
	private class AsyncAddUser extends AsyncTask<Object, Object, Object>{
		String res = "";
		
		@Override
		protected Object doInBackground(Object... params) {
			try {  
				res = CustomHttpClient.executeHttpGet("http://cs346.cs.uwosh.edu/students/blairm83/cs342group6/addUser.php?user="+newUser+"&pass="+newPass); 
				res = res.toString().trim();  
			} catch (Exception e) {  }
			return null;
		}
		@Override
		protected void onPostExecute(Object result){
			if(res.equals("1")){
				Toast.makeText(getApplicationContext(), "User created", Toast.LENGTH_SHORT).show();
				MainActivity.setUserID(newUser);
				Intent myIntent = new Intent("android.intent.action.MAP");
				startActivity(myIntent);
			}else{
				Toast.makeText(getApplicationContext(), "Can not add user. Sorry!", Toast.LENGTH_SHORT).show();
			}
		}
	}

	
//	private class AsyncCheckNewUser extends AsyncTask<Object, Object, Object>{
//		String response = "";
//		StringTokenizer tk;
//		
//		@Override
//		protected Object doInBackground(Object... arg0) {			
//			//  Connect and get response string through CustomHttpClient
//			try {  
//				response = CustomHttpClient.executeHttpGet("http://cs346.cs.uwosh.edu/students/blairm83/cs342group6/viewUsers.php"); 
//				response = response.toString().trim();
//			} catch (Exception e) {  }
//			return null;
//		}
//		@Override
//		protected void onPostExecute(Object result){
//			tk = new StringTokenizer(response);
//			boolean alreadyExists = false;
//			while(!alreadyExists && tk.hasMoreTokens()){
//				if(tk.nextToken().equals(newUser)){
//					alreadyExists = true;
//				}
//			}
//			
//			if(!alreadyExists){
//				//add new user (create asyncTask for new user)
//				new AsyncAddUser().execute((Object[]) null);
//			}else{
//				Toast.makeText(getApplicationContext(), "User already exists", Toast.LENGTH_SHORT).show();
//			}
//		}
//		
//	}
	
}

